package com.itlxd.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itlxd.entity.ShoppingCart;
import com.itlxd.global.SysConstants;
import com.itlxd.global.SysMessages;
import com.itlxd.mapper.ShoppingCartMapper;
import com.itlxd.service.IShoppingCartService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 * 购物车 服务实现类
 * </p>
 *
 * @author ${author}
 * @since 2021-12-25
 */
@Service
public class ShoppingCartServiceImpl implements IShoppingCartService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    @Cacheable(cacheNames = SysConstants.SHOPPING_CART_CACHE, key = "#userId")
    public List<ShoppingCart> listShoppingCarts(Long userId) {
        LambdaQueryWrapper<ShoppingCart> lambdaQuery = new LambdaQueryWrapper<>();
        lambdaQuery.eq(null != userId, ShoppingCart::getUserId, userId)
                .orderByDesc(ShoppingCart::getCreateTime);
        return shoppingCartMapper.selectList(lambdaQuery);
    }

    @Override
    @CacheEvict(cacheNames = SysConstants.SHOPPING_CART_CACHE, key = "#food.userId")
    public Integer insertShoppingCart(ShoppingCart food) {
        LambdaQueryWrapper<ShoppingCart> lambdaQuery = new LambdaQueryWrapper<>();
        lambdaQuery.eq(null != food.getDishId(), ShoppingCart::getDishId, food.getDishId())
                .eq(null != food.getSetmealId(), ShoppingCart::getSetmealId, food.getSetmealId())
                .eq(null != food.getDishFlavor(), ShoppingCart::getDishFlavor, food.getDishFlavor())
                .eq(null != food.getUserId(), ShoppingCart::getUserId, food.getUserId());
        ShoppingCart shoppingCart = shoppingCartMapper.selectOne(lambdaQuery);
        //如果查询数据库有值，修改数据库数量
        if (null != shoppingCart) {
            shoppingCart.setNumber(shoppingCart.getNumber() + 1);
            int updateRows = shoppingCartMapper.updateById(shoppingCart);
            return updateRows;
        }
        //如果数据库中没有值，则插入新数据
        int insertRows = shoppingCartMapper.insert(food);
        return insertRows;
    }

    @CacheEvict(cacheNames = SysConstants.SHOPPING_CART_CACHE, key = "#food.userId")
    @Override
    public Integer updateShoppingCart(ShoppingCart food) {
        LambdaQueryWrapper<ShoppingCart> lambdaQuery = new LambdaQueryWrapper<>();
        lambdaQuery.eq(null != food.getDishId(), ShoppingCart::getDishId, food.getDishId())
                .eq(null != food.getSetmealId(), ShoppingCart::getSetmealId, food.getSetmealId())
                .eq(null != food.getDishFlavor(), ShoppingCart::getDishFlavor, food.getDishFlavor())
                .eq(null != food.getUserId(), ShoppingCart::getUserId, food.getUserId());
        ShoppingCart shoppingCart = shoppingCartMapper.selectOne(lambdaQuery);
        //如果查询数据库有值，且数量>1，修改数据库数量
        if (null != shoppingCart && shoppingCart.getNumber() > SysConstants.NUM_ONE) {
            shoppingCart.setNumber(shoppingCart.getNumber() - 1);
            int updateRows = shoppingCartMapper.updateById(shoppingCart);
            return updateRows;
        }
        //如果菜品数量==1，则删除购物车信息
        int delRows = shoppingCartMapper.deleteById(shoppingCart.getId());

        return delRows;
    }

    @CacheEvict(cacheNames = SysConstants.SHOPPING_CART_CACHE, key = "#userId")
    @Override
    public Integer deleteShoppingCart(Long userId) {
        LambdaQueryWrapper<ShoppingCart> lambdaQuery = new LambdaQueryWrapper<>();
        lambdaQuery.eq(ShoppingCart::getUserId, userId);
        return shoppingCartMapper.delete(lambdaQuery);
    }
}
